import syscall
import strings
import libc_temp

type time_t = struct {
    i64 sec
    i64 nsec
    ptr<libc_tm_t> tm
    var timestamp = fn(self s):i64 {
        return s.sec
    }
    var datetime = fn(self s):string {
        var buf = vec<u8>{len=100} // 必须预留足够大的空间,才能正常引用

        var len = strftime(buf.ref(), buf.len, '%Y-%m-%d %H:%M:%S'.ref(), s.tm)
        buf = buf.slice(0, len)

        return buf as string
    }
}

// 将 time_t 使用当前时间进行初始化
fn now():time_t {
    var sys_time = syscall.clock_gettime(syscall.CLOCK_REALTIME)
    var t = time_t {
        sec = sys_time.sec,
        nsec = sys_time.nsec,
        tm = localtime(&sys_time.sec)
    }

    return t
}
